package com.yl.solution.dao.impl;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;












import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yl.solution.dao.Resolved_ProblemDao;
import com.yl.solution.entity.Resolved_Problem;
import com.yl.solution.util.PaginationSupport;


public class Resolved_ProblemDaoImpl extends HibernateDaoSupport implements Resolved_ProblemDao {
	
	/*private BaseDAOImpl<Resolved_Problem> dao = new BaseDAOImpl<Resolved_Problem>();
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}*/

	

	/*@SuppressWarnings("unchecked")
	@Override
	public List<Resolved_Problem> findCount() {
		
		String sql = "select * from Resolved_Problem";
		List<Resolved_Problem> list = sessionFactory.getCurrentSession().createSQLQuery(sql).list();  
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Resolved_Problem> findByNameForType(String name, Integer type) {
		StringBuffer sql = new StringBuffer("select * from Resolved_Problem where 1=1");
		List<Object> param = new ArrayList<>();
		StringBuffer sb = new StringBuffer();
		sb.append(name);
		List<String>  list = new ArrayList<String>();
		for (int i = 0; i < (sb.length())/2; i++) {
			String s = sb.substring(2*i,(2*i)+2);
			list.add(s);
		}
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i) != null && !"".equals(list.get(i))){
				sql.append("and sketch = ?");
				param.add(list.get(i));
			}
		}
		if( !"".equals(type)&& type != null){
			sql.append("and question_type = ?");
			param.add(type);
		}
		Query query = sessionFactory.getCurrentSession().createSQLQuery(sql.toString());
		if (param != null && param.size() >0) {
			for (int i = 0; i < param.size(); i++) {
				query.setParameter(i, param.get(i));
			}
		}
		return query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Resolved_Problem> findBySearch_frequency(Integer search_frequency) {
		StringBuffer sql =new StringBuffer("select * from Resolved_Problem where 1=1");
		List<Object> param = new ArrayList<>();
		if(search_frequency != null && !"".equals(search_frequency)){
			sql.append("and sketch = ?");
			param.add(search_frequency);
		}
		Query query = sessionFactory.getCurrentSession().createSQLQuery(sql.toString());
		if (param != null && param.size() >0) {
			for (int i = 0; i < param.size(); i++) {
				query.setParameter(i, param.get(i));
			}
		}
		return query.list();
	}*/

	/*public BaseDAOImpl<Resolved_Problem> getDao() {
		return dao;
	}

	public void setDao(BaseDAOImpl<Resolved_Problem> dao) {
		this.dao = dao;
	}*/



	@SuppressWarnings("unchecked")
	@Override
	public List<Resolved_Problem> selectById(Integer id) {
		return (List<Resolved_Problem>) getHibernateTemplate().find("select * from Resolved_Problem where id=?", id);
	}

	

	@SuppressWarnings("unchecked")
	@Override
	public List<Resolved_Problem> getResolved_ProblemByInfo(final Map map,
		final	int currPageNo,final int pageSize) {
		List<Resolved_Problem> list = super.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String sql = "from Resolved_Problem R where 1=1 and (";
				Integer question_type = null; 			//问题类型
				List<Object> param = new ArrayList<>();
				List<String>  list = new ArrayList<String>();
				if(map!=null){
					if(map.get("sketch")!=null){
						StringBuffer sb = new StringBuffer();
						sb.append(map.get("sketch").toString());
						for (int i = 0; i < (sb.length())/2; i++) {
							String s = sb.substring(2*i,(2*i)+2);
							list.add(s);
						}
						for (int i = 0; i < list.size(); i++) {
							if(list.get(i) != null && !"".equals(list.get(i))){
								sql+=" R.sketch = ? or";
								param.add(list.get(i));
								
							}
						}
					}
					if(map.get("question_type")!=null){
						sql+=" R.question_type = :question_type )";
						question_type = (Integer) map.get("question_type");
					}else {
						sql+="1=1)";
					}
				}
				sql += " order by R.search_frequency desc";
				Query query = session.createQuery(sql);
				if (param != null && param.size() >0) {
					for (int i = 0; i < param.size(); i++) {
						query.setParameter(i, param.get(i));
					}
				}
				if(question_type != null)query.setParameter("question_type", question_type);
				query.setFirstResult((currPageNo-1) * pageSize);
				query.setMaxResults(pageSize);
				return query.list();
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getResolved_ProblemByCount(final Map map) {
		List<Resolved_Problem> list = super.getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String sql = "from Resolved_Problem R where 1=1 and (";
				String sketch = null; 					//问题简述
				Integer question_type = null; 			//问题类型
				List<Object> param = new ArrayList<>();
				List<String>  list = new ArrayList<String>();
				if(map!=null){
					if(map.get("sketch")!=null){
						StringBuffer sb = new StringBuffer();
						sb.append(map.get("sketch").toString());
						for (int i = 0; i < (sb.length())/2; i++) {
							String s = sb.substring(2*i,(2*i)+2);
							list.add(s);
						}
						for (int i = 0; i < list.size(); i++) {
							if(list.get(i) != null && !"".equals(list.get(i))){
								sql+=" R.sketch = ? or";
								param.add(list.get(i));
								
							}
						}
					}
					if(map.get("question_type")!=null){
						sql+=" R.question_type = :question_type )";
						question_type = (Integer) map.get("question_type");
					}else {
						sql+="1=1)";
					}
				}
				sql += " order by R.search_frequency desc";
				Query query = session.createQuery(sql);
				if (param != null && param.size() >0) {
					for (int i = 0; i < param.size(); i++) {
						query.setParameter(i, param.get(i));
					}
				}
				if(question_type != null)query.setParameter("question_type", question_type);
				return query.list();
			}
		});
		return list.size();
	}

	
}
